import Vue from 'vue'
import VueRouter from 'vue-router'
import  NProgress  from 'nprogress'
import "nprogress/nprogress.css"

Vue.use(VueRouter)

const routes = [
  {
    path: '/Home',
    name: "Home",
    component: () => import('@/views/Home.vue'),
    children: [
      {
        path: '/System/DistributeRole/:userId',
        component: () => import('@/views/DistributeRole.vue')
      },
      {
        path: '/System/DistributePermission/:roleId',
        component: () => import('@/views/DistributePermission.vue')
      }
    ]
  },
  {
    path: "/Login",
    component: () => import("@/views/Login.vue")
  },
  {
    path: "*",
    component: () => import("@/views/NotFound.vue")
  }
]

const router = new VueRouter({
  routes
})

// 添加路由守卫
router.beforeEach((to, from, next) => {

  // 如果请求的是 /Login , 则直接放行
  if (to.path == "/Login") {
    next();
    return;
  }

  // 如果用户没有登录，则直接返回，也就是不放行
  let userId = sessionStorage.getItem("userId");
  if (!userId) {
    router.push("/Login")
    return;
  }
  // 如果用户已经登录过了，则直接放行
  next();
})

router.beforeEach((to, from, next) => {
  NProgress.start();
  next();
})
router.afterEach((to, from, next) => {
  NProgress.done();  
})

export default router
